﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Rainbow.ObjectFlow.Framework;

namespace TestDoc
{
    internal class PrettyPrinting : BasicOperation<Program.TestReport>
    {
        private const string MessageHeader = "TestDoc.Messages.Header.txt";

        public override Program.TestReport Execute(Program.TestReport data)
        {
            DisplayTitle(data.Assembly);
            SendOuputToConsole(data.Tests);
            return data;
        }

        private static void DisplayTitle(Assembly thisExe)
        {
            string headerText = GetResourceText(thisExe, MessageHeader);
            Console.WriteLine(headerText);
        }

        private static string GetResourceText(Assembly thisExe, string resourceName)
        {
            string headerText;
            using (var header = thisExe.GetManifestResourceStream(resourceName))
            {
                using (TextReader reader = new StreamReader(header))
                {
                    headerText = reader.ReadToEnd();
                }
            }
            return headerText;
        }

        private static void SendOuputToConsole(IEnumerable<MethodInfoSmall> output)
        {
            foreach (var typeInfo in output)
            {
                SendToConsole(typeInfo);
            }
        }

        private static void SendToConsole(MethodInfoSmall element)
        {
            if (element.TypeCode == MethodType.ClassName)
                Console.WriteLine(string.Format("     {0}", element.TypeName));
            else if (element.TypeCode == MethodType.Namespace)
            {
                Console.WriteLine(element.TypeName);
            }
            else
            {
                Console.WriteLine(string.Format("         {0}", element.TypeName));
            }
        }
    }
}
